const { Service } = require("egg")
class UserService extends Service {
  async list(pageSize, pageNum, where) {
    const data = await this.app.mysql.select("egg_user", {
      where,
      orders: [
        ["id", "desc"],
        ["userName", "desc"],
      ],
      offset: (pageNum - 1) * pageSize,
      limit: pageSize,
    })
    const total = await this.app.mysql.count("egg_user", where)
    data.map((item) => {
      delete item.password
    })
    return { data, total }
  }
  async isExist(userName) {
    //判断数据库是否已经存在当前添加的用户
    const exists = await this.app.mysql.query(
      `SELECT egg_user.userName FROM egg_user WHERE userName LIKE ?`,
      [userName]
    )
    return exists
  }
  async single(id) {
    const result = await this.app.mysql.get("egg_user", { id })
    return result
  }
  async create(user) {
    const result = await this.app.mysql.insert("egg_user", user)
    return result
  }
  async update(user) {
    const result = await this.app.mysql.update("egg_user", user)
    return result
  }
  async destroy(id) {
    const result = await this.app.mysql.delete("egg_user", { id })
    return result
  }
  async wxinfo(openid) {
    const result = await this.app.mysql.get("egg_wxuser", { openid })
    return result
  }
}
module.exports = UserService
